package com.wgchao;

import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.JWTValidator;
import cn.hutool.jwt.RegisteredPayload;
import cn.hutool.jwt.signers.JWTSigner;
import cn.hutool.jwt.signers.JWTSignerUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: JwtTest
 * @author: wgchao
 * @createTime: 2021/11/19 9:42 AM
 * @Description: TODO
 */
public class JwtTest {


    public static void main(String[] args) throws Exception{
        Map<String, Object> map = new HashMap<String, Object>() {
            private static final long serialVersionUID = 1L;
            {
                put("uid", Integer.parseInt("123"));
                put("uname", "admin");
                put("organCode", "37020201");
                put(RegisteredPayload.EXPIRES_AT, DateUtil.beginOfDay(DateUtil.yesterday()));
            }
        };
        JWTSigner signer = JWTSignerUtil.hs256("admin".getBytes());
        String token = JWTUtil.createToken(map, signer);
        System.out.println(token);
        JWT jwt = JWTUtil.parseToken(token);
        JWTSigner signer1 = JWTSignerUtil.hs256("admin".getBytes());
        System.out.println(JWTUtil.verify(token, signer1));
        System.out.println(JWTValidator.of(token).validateAlgorithm(signer1).validateDate());
        System.out.println(jwt.getPayload("uname"));
        System.out.println(JWTUtil.parseToken(token).getPayloads());

        String token1 = JWT.create()
                // 设置签发时间
                .setExpiresAt(DateUtil.beginOfDay(DateUtil.yesterday()))
                .setKey("123456".getBytes())
                .sign();
        System.out.println(JWTUtil.parseToken(token1).getPayloads());
    }

}
